package com.zzptc.lzh.myapp9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzptc.lzh.myapp9.Service.MyAppService;
import com.zzptc.lzh.myapp9.entity.RecipeModel;
import com.zzptc.lzh.myapp9.entity.ResultsBean;

import org.litepal.LitePal;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    List<ResultsBean> allResultsBean;
    private static final String TAG = "MainActivity";
    //边定义边实例化retrofit
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.lv);

        getRecipInfo();
        getRecipInfoFromDB();

        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);
    }

    private void getRecipInfoFromDB() {
        allResultsBean = LitePal.findAll(ResultsBean.class);
    }

    private void getRecipInfo() {
        //构造接口对象
        MyAppService myAppService = retrofit.create(MyAppService.class);
        //调用接口中的getAll方法，返回Call类型
        Call<RecipeModel> all = myAppService.getAll(5);
        all.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                RecipeModel body = response.body();
                Log.d(TAG, "onResponse: "+body.getResults().size());
                //将数据放入sqlite数据库中，使用litepal数据库框架
                List<ResultsBean> results=body.getResults();
                for (ResultsBean r:results){
                    r.save();
                    Log.d(TAG, "onResponse: "+r.save());

                }
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {

            }
        });
    }

    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return allResultsBean.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v=View.inflate(MainActivity.this,R.layout.recipemodel_item,null);//局部变量必须得初始化才能用
            ImageView iv_tupian=v.findViewById(R.id.iv_tupian);
            TextView tv_caiming=v.findViewById(R.id.tv_caiming);
            //赋值，值从哪里来
            ResultsBean resultsBean=allResultsBean.get(position);
            String cover = resultsBean.getCover();
            Glide.with(MainActivity.this)
                    .load(cover)
                    .into(iv_tupian);
            tv_caiming.setText(resultsBean.getTitle());
            return v;
        }
    }
}
